from src.core.payment_client.api import PaymentAPIImpl


class FondyPaymentClient(PaymentAPIImpl):
    def __init__(self, api_token: str, shop_id: str, server_callback_url: str = None):
        super().__init__(api_token, shop_id, base_url="https://api.fondy.eu/api")
        self.server_callback_url = server_callback_url

    async def create_bill(
        self,
        amount: float,
        order_id: str,
        order_description: str = "",
        currency_in: str = "USD",
        language: str = "en",
        response_url: str = None,
    ):
        """
        Создаем оплату
        @param amount: Сумма в центах
        @param order_id: Номер заказа
        @param order_description: Описание заказа
        @param currency_in: Валюта (USD, RUB, UAH)
        @param language: Язык оплаты (ru, uk, en)
        @param response_url: Ссылка для редиректа после оплаты

        POST https://api.fondy.eu/api/checkout/url/
        """

        data = {
            "request": {
                "server_callback_url": self.server_callback_url,
                "response_url": response_url,
                "lang": language,
                "order_id": order_id,
                "currency": currency_in,
                "merchant_id": str(self.shop_id),
                "order_desc": order_description,
                "amount": str(amount).replace(".", ""),
            }
        }
        return await self.send_signed_request("POST", "/checkout/url/", data)

    async def order_status(self, order_id):
        """
        Проверяем статус оплаты
        https://portal.fondy.eu/en/info/api/v1.0/5#chapter-5-4-json
        """

        data = {
            "request": {
                "order_id": order_id,
                "merchant_id": str(self.shop_id),
            }
        }

        return await self.send_signed_request("POST", "/status/order_id", data)
